package com.source.lightfowl.basedata.controller;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.PoultryParentDownloadDTO;
import com.source.lightfowl.DTO.PoultryParentUploadDTO;
import com.source.lightfowl.authorization.CheckPermissions;
import com.source.lightfowl.basedata.entity.Poultry_Parent;
import com.source.lightfowl.basedata.service.PoultryParentService;
import com.source.lightfowl.basedata.service.UploadExcelService;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author: Faink
 * @CreateTime: 2020/10/27 19:50
 */
@Api(value = "父级禽类管理",tags = "父级禽类管理")
@RestController
@CrossOrigin
@RequestMapping("/poultryParent")
public class PoultryParentController {

    @Autowired
    private PoultryParentService poultryParentService;

    @ApiOperation(value = "添加父级禽类")
    @PostMapping("/add")
    //@CheckPermissions(value = "")
    public MyResult add(Poultry_Parent parent) {
        int save = poultryParentService.save(parent);
        if (save == 1)
            return MyResult.build(StatusCode.SUCCESS, "添加成功");
        else
            return MyResult.build(StatusCode.REPEATDATA, "数据重复");
    }

    @ApiOperation(value = "逻辑删除父级禽类")
    @DeleteMapping("/delete/id")
    public MyResult delete(String id){
        poultryParentService.deleteById(id);
        return MyResult.build(StatusCode.SUCCESS, "删除成功");
    }

    @ApiOperation(value = "编辑父级禽类")
    @PostMapping("/update")
    //@CheckPermissions(value = "")
    public MyResult update(Poultry_Parent parent) {
        int save = poultryParentService.updateById(parent);
        if (save == 1)
            return MyResult.build(StatusCode.SUCCESS, "编辑成功");
        else
            return MyResult.build(StatusCode.REPEATDATA, "数据重复");
    }

    @ApiOperation(value = "查找所有父级")
    @GetMapping("/findAll")
    public MyResult findAll(){
        return MyResult.build(StatusCode.SUCCESS, "操作成功", poultryParentService.findAll());
    }

    @ApiOperation(value = "回显")
    @GetMapping("/findOne")
    public MyResult findOne(String id){
        return MyResult.build(StatusCode.SUCCESS, "操作成功", poultryParentService.findOne(id));
    }

    @ApiOperation(value = "父级禽类Excel上传")
    @PostMapping("/upload")
    public MyResult upload(MultipartFile file, HttpServletRequest request){
        String fileName = file.getOriginalFilename(); //文件名
        String fileSuffix = fileName.substring(fileName.length()-5); //.xlsx
        String fileSuffix1 = fileName.substring(fileName.length()-4); //.xls
        if (!fileSuffix.equals(".xlsx")&&!fileSuffix1.equals(".xls")){
            return MyResult.build(500,"文件格式错误");
        }
        try {
            EasyExcel.read(file.getInputStream(), PoultryParentUploadDTO.class,new UploadExcelListener(poultryParentService,request)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.no();
        }
        return MyResult.ok();
    }

    @ApiOperation(value = "父级禽类Excel下载")
    @GetMapping("/download")
    public MyResult download(HttpServletResponse response){
        try {
            DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
            downloadExcelHandler.download(response,poultryParentService, PoultryParentDownloadDTO.class);
        }catch (Exception e){
            return MyResult.no();
        }
        return MyResult.ok();
    }

}
